package com.byb.server.service.impl;

import com.byb.server.service.WordKeyService;
import com.byb.server.domain.WordKey;
import com.byb.server.repository.WordKeyRepository;
import com.byb.server.repository.search.WordKeySearchRepository;
import com.byb.server.service.dto.WordKeyDTO;
import com.byb.server.service.mapper.WordKeyMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing WordKey.
 */
@Service
@Transactional
public class WordKeyServiceImpl implements WordKeyService{

    private final Logger log = LoggerFactory.getLogger(WordKeyServiceImpl.class);

    private final WordKeyRepository wordKeyRepository;

    private final WordKeyMapper wordKeyMapper;

    private final WordKeySearchRepository wordKeySearchRepository;
    public WordKeyServiceImpl(WordKeyRepository wordKeyRepository, WordKeyMapper wordKeyMapper, WordKeySearchRepository wordKeySearchRepository) {
        this.wordKeyRepository = wordKeyRepository;
        this.wordKeyMapper = wordKeyMapper;
        this.wordKeySearchRepository = wordKeySearchRepository;
    }

    /**
     * Save a wordKey.
     *
     * @param wordKeyDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public WordKeyDTO save(WordKeyDTO wordKeyDTO) {
        log.debug("Request to save WordKey : {}", wordKeyDTO);
        WordKey wordKey = wordKeyMapper.toEntity(wordKeyDTO);
        wordKey = wordKeyRepository.save(wordKey);
        WordKeyDTO result = wordKeyMapper.toDto(wordKey);
       // wordKeySearchRepository.save(wordKey);
        return result;
    }

    /**
     *  Get all the wordKeys.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WordKeyDTO> findAll(Pageable pageable) {
        log.debug("Request to get all WordKeys");
        return wordKeyRepository.findAll(pageable)
            .map(wordKeyMapper::toDto);
    }

    /**
     *  Get one wordKey by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public WordKeyDTO findOne(Long id) {
        log.debug("Request to get WordKey : {}", id);
        WordKey wordKey = wordKeyRepository.findOne(id);
        return wordKeyMapper.toDto(wordKey);
    }

    /**
     *  Delete the  wordKey by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete WordKey : {}", id);
        wordKeyRepository.delete(id);
        //wordKeySearchRepository.delete(id);
    }

    /**
     * Search for the wordKey corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WordKeyDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of WordKeys for query {}", query);
        Page<WordKey> result = wordKeySearchRepository.search(queryStringQuery(query), pageable);
        return result.map(wordKeyMapper::toDto);
    }
}
